<script setup lang="ts">
import { tableConfig } from './config/table-config';
import { doAddCustomerInformation } from '@/api/base/customerInformation';
import { Message } from '@/instance';
import useEmit from '@/hooks/useEmitter';
import usePageSave from '@/hooks/usePageSave';
import type { CustomerInformationListInfo } from '@/api/base/customerInformation/model/type';

defineOptions({
    name: 'CustomerInformationAdd'
});

const STORE_KEY = 'customerAddData'; // store数据的key
const { loading, modalOk, modalSlotRef: tableRef } = useModal();

const { startRecording, isUpdate } = useEmit(STORE_KEY); // 页面标签路由关闭二次确认事件监听

useRecordData(tableRef, STORE_KEY, isUpdate, startRecording); // 复制客户信息

/**
 * 保存新增数据
 */
const addData = async () => {
    let data: CustomerInformationListInfo[] = tableRef.value.getSaveData();

    try {
        const res = await doAddCustomerInformation(data);
        Message.success('操作成功！');
        return res;
    } catch (error) {
        console.error(error);
    }
};

/**
 * 点击保存按钮
 */
const handleOk = async () => {
    try {
        await modalOk(addData);
        usePageSave('/base');
    } catch (e) {
        console.log(e);
    }
};
</script>

<template>
    <q-page>
        <base-table
            ref="tableRef"
            :add-table-row="true"
            :open-sticky="true"
            :show-fullscreen="true"
            :tableConfig="tableConfig"
        >
            <!-- 保存/取消 -->
            <template #tableHeadEnd>
                <!-- <el-button class="m-r-16">取消</el-button> -->
                <q-submit-button
                    size="default"
                    :hide-label="true"
                    align="right"
                    :horizontal="true"
                    :cancel-button="false"
                    :loading="loading"
                    @save="handleOk"
                />
            </template>
        </base-table>
    </q-page>
</template>

<style lang="scss" scoped></style>
